package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

type TranceCode string

func worker(ctx context.Context) {
	key := TranceCode("chx")
	value := ctx.Value(key).(string)
	defer wg.Done()
	go worker2(ctx)
LABEL:
	for {
		fmt.Printf("在工作中..%s\n", value)
		time.Sleep(time.Second)
		select {
		case <-ctx.Done():
			break LABEL
		default:
		}
	}
	fmt.Println("退出工作")

}
func worker2(ctx context.Context) {
	key := TranceCode("chx")
	value := ctx.Value(key).(string)
LABEL:
	for {
		fmt.Printf("worker2在工作中..%s\n", value)
		time.Sleep(time.Second)
		select {
		case <-ctx.Done():
			break LABEL
		default:
		}
	}
	fmt.Println("退出工作")
	defer wg.Done()
}
func main() {
	ctx, cancel := context.WithCancel(context.Background())
	ctx = context.WithValue(ctx, TranceCode("chx"), "123451")
	wg.Add(2)
	go worker(ctx)
	time.Sleep(time.Second * 5)
	cancel()
	wg.Wait()
	fmt.Println("over...")
}
